package com.psa.system;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.psa.dialog.WaitingDialog;
import com.psa.element.IJson;
import com.psa.element.Merchandise;
import com.psa.element.RequestCmd;
import com.psa.element.RequestMessage;
import com.psa.tool.*;
import com.wxw.BaseLibrary.adapter.DragListAdapter;
import com.wxw.BaseLibrary.component.DragListView;
import com.wxw.BaseLibrary.element.JsonObject;
import com.wxw.BaseLibrary.element.JsonProperty;
import com.wxw.BaseLibrary.tool.ActivityTool;
import com.wxw.BaseLibrary.tool.Timeout;
import net.duohuo.dhroid.ioc.InjectUtil;
import net.duohuo.dhroid.ioc.annotation.InjectView;

/**
 *
 * @author david
 * @date 2014/5/14
 */
public class DefaultMerchandiseActivity extends BaseActivity implements IHttpCallback {
    @InjectView(id = R.id.merchandise_list)
    private DragListView merchandise_list;

    private String RMsgKey = "saveMerList";
    private String CallbackMsg = "";
    private int ResultNo = 0;
    private WaitingDialog waitingDialog = null;
    private RequestMessage requestMessage = null;

    private Runnable showHttpCallback = new Runnable() {
        @Override
        public void run() {
            if (waitingDialog != null) {
                waitingDialog.stop();
            }
            SystemStatus.showMessage(DefaultMerchandiseActivity.this, CallbackMsg);
            if (ResultNo==1) {
                setResult(ResultNo);
                finish();
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTool.hideTitle(this);
        ActivityTool.fullScreen(this);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.defaultmerchandise_view);
        InjectUtil.inject(this);
        DragListAdapter<com.wxw.BaseLibrary.element.ElementStatus> dragListAdapter = new DragListAdapter<com.wxw.BaseLibrary.element.ElementStatus>(this, SystemStatus.DefaultMerchandises);
        dragListAdapter.setDragImageID(R.drawable.list_icon);
        merchandise_list.setAdapter(dragListAdapter);
        tv_info.setText(SystemStatus.getReqCount());
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.btn_ok){
            //do save
            waitingDialog = new WaitingDialog(this, getString(R.string.app_sys_info),
                    getString(R.string.default_merchandise_dia_text), 180, new Timeout.OnTimeoutListener() {
                @Override
                public void onTimeout(Object sender, int time) {
                    requestMessage.Cancel = true;
                }
            });
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.setProperty("MerSeq", new JsonProperty());
                        DBTool.getInstance().execSQL("Delete From MerchandiseSeq");
                        for (com.wxw.BaseLibrary.element.ElementStatus elementStatus : SystemStatus.DefaultMerchandises) {
                            JsonProperty jsonProperty = jsonObject.getProperty("MerSeq").add(new JsonObject());
                            jsonProperty.setProperty("ID",new JsonProperty("'"+ ((Merchandise) elementStatus.getObject()).ID+"'"));
                            jsonProperty.setProperty("Index", new JsonProperty(elementStatus.getIndex()+1));
                            DBTool.getInstance().execSQL("Insert Into MerchandiseSeq(MerchandiseID,SequenceNo) Values('" + ((Merchandise) elementStatus.getObject()).ID + "'," + elementStatus.getIndex() + ")");
                        }
                        requestMessage = new RequestMessage(SystemStatus.BUSINESS_URL, SystemStatus.BusinessWSMethod,RequestCmd.create("savemerchandiselist", jsonObject.toString()), RequestCmd.class, DefaultMerchandiseActivity.this, RequestMessage.HTTP_METHOD.POST);
                        requestMessage.MessageKey = RMsgKey;
                        RequestManager.getRequestManager().addMessage(requestMessage);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e(SystemStatus.TAG, e.getMessage());
                    }
                }
            }).start();
        } else {
            RequestManager.getRequestManager().removeGeneralEventListener(this);
            setResult(ResultNo);
            finish();
        }
    }

    @Override
    public void callCompleted(RequestMessage message, IJson requestCmd) {
        if (message.MessageKey.equals(RMsgKey)) {
            ResultNo = 1;
            CallbackMsg = ((RequestCmd)requestCmd).Result;
            DefaultMerchandiseActivity.this.runOnUiThread(showHttpCallback);
        }
    }

    @Override
    public void callError(RequestMessage message, String err) {
        CallbackMsg = getString(R.string.app_error_msg)+ err;
        DefaultMerchandiseActivity.this.runOnUiThread(showHttpCallback);
    }
}
